<?php
session_start();
include_once "../apps/ManagerIUDS.php";
$managerIUDS = new ManagerIUDS();

$breedName = $_POST["breed_name"];
$breedDetail = $_POST["breed_detail"];
$speciesId = $_POST["species_id"];

$breedId = $_GET["breed_id"];
if($breedId == "" || $breedId == null){
	$breedId = $_POST["breed_id"];
}

$mode = $_GET["mode"];
if($mode == "" || $mode == null){
	$mode = $_POST["mode"];
}

$page = $_GET['page'];
if (!isset($page) && $page == "") {
	$page = 1;
}

if($mode == "insert"){
	$arrData["breed_name"] = "".$breedName."";
	$arrData["breed_detail"] ="".$breedDetail."";
	$arrData["species_id"] ="".$speciesId."";
	$arrData["create_date"] = "".date("Y-m-d")."";
	$arrData["create_by"] = "".$_SESSION["user_name"]."";
	$managerIUDS->inserData($arrData, "breed");
	header("Location:../contents/breed_main.php");
}else if($mode == "delete"){
	$arrPrimaryKey["breed_id"] = "".$breedId."";
	$managerIUDS->deleteData("breed", $arrPrimaryKey, "");
	header("Location:../contents/breed_main.php");
}else if($mode == "update"){
	$arrData["breed_name"] = "".$breedName."";
	$arrData["breed_detail"] ="".$breedDetail."";
	$arrData["species_id"] ="".$speciesId."";
	$arrData["update_date"] = "".date("Y-m-d")."";
	$arrData["update_by"] = "".$_SESSION["user_name"]."";
	$arrPrimaryKey["breed_id"] = "".$breedId."";
	//print_r($arrData);
	$managerIUDS->updateData($arrData, "breed", $arrPrimaryKey, "");
	header("Location:../contents/breed_main.php");
}else if($mode == "search"){
	$breedName = $_GET["breed_name"];
	header("Location:../contents/breed_main.php?mode=search&page=".$page."&breed_name=".urlencode($breedName));
}else if($mode == "listBreedBySpeciesId"){
	$speciesId = $_GET["species_id"];
	$breedData = array();
	$breedData = $managerIUDS->queryData("breed_name,breed_id", "breed", " WHERE species_id='".$speciesId."'", "2", "");
	$strJson .= '{"breedData" : [';
	//$strJson .= '{"breedId":"0",';
	//$strJson .= '"breedName":"--- Select ---"}, ';
	foreach ($breedData as $value){
		$strJson .= '{"breedId":"'.$value["breed_id"].'",';
		$strJson .= '"breedName":"'.$value["breed_name"].'"}, ';
	}
	$strJson = substr($strJson, 0,strlen($strJson)-2);
	$strJson .=" ]}";
	echo $strJson;
	//echo json_encode($strJson);
}else if($mode == "listBreedData"){
	$breedData = array();
	$breedName = $_GET["breed_name"];
	if($breedName == "" || $breedName == null){
		$breedName = $_POST["breed_name"];
	}
	$breedData = $managerIUDS->queryData("breed_name", "breed", " WHERE breed_name LIKE '%".$breedName."%'", "2", "");
	if(sizeof($breedData)<=0){
		$breedData = "";
	}
	echo json_encode($breedData);
}
?>